package com.clientmanage.service.impl;

import com.clientmanage.domain.bean.CustomerCareReminder;
import com.clientmanage.domain.resp.PageResult;
import com.clientmanage.domain.vo.clientCareReminderVo;
import com.clientmanage.mapper.CustomerCareReminderMapper;
import com.clientmanage.service.ICustomerCareReminderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import com.clientmanage.domain.bean.Customer;
import com.clientmanage.domain.bean.Pet;
import com.clientmanage.mapper.CustomerMapper;
import com.clientmanage.mapper.PetMapper;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HUANG
 * @since 2025-06-30
 */
@Service
public class CustomerCareReminderServiceImpl extends ServiceImpl<CustomerCareReminderMapper, CustomerCareReminder> implements ICustomerCareReminderService {

    @Autowired
    CustomerCareReminderMapper customerCareReminderMapper;

    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private PetMapper petMapper;

    @Override
    public PageResult<clientCareReminderVo> pageQuery(String customerName, String petName, String careType, String status, int pageNum, int pageSize) {
        List<clientCareReminderVo> list = customerCareReminderMapper.selectPageVo(customerName, petName, careType, status);
        return new PageResult<>((long) pageSize, (long) pageNum, list, (long) list.size());
    }

    @Override
    public void saveOrUpdateCare(clientCareReminderVo vo) {
        // 新增时校验客户和宠物ID
        if (vo.getId() == null) {
            Customer customer = customerMapper.selectById(vo.getCustomerId());
            if (customer == null) {
                throw new IllegalArgumentException("客户不存在");
            }
            Pet pet = petMapper.selectById(vo.getPetId());
            if (pet == null || !pet.getCustomerId().equals(vo.getCustomerId())) {
                throw new IllegalArgumentException("宠物不存在或不属于该客户");
            }
        }
        // 发送方式去重、去空、排序
        String sendMethod = vo.getSendMethod();
        if (sendMethod != null && !sendMethod.isEmpty()) {
            sendMethod = java.util.Arrays.stream(sendMethod.split(","))
                .map(String::trim)
                .filter(s -> !s.isEmpty())
                .distinct()
                .sorted()
                .reduce((a, b) -> a + "," + b).orElse("");
            vo.setSendMethod(sendMethod);
        }
        CustomerCareReminder entity = new CustomerCareReminder();
        BeanUtils.copyProperties(vo, entity);
        if (vo.getId() == null) {
            this.save(entity);
        } else {
            this.lambdaUpdate().eq(CustomerCareReminder::getId, vo.getId()).update(entity);
        }
    }

    @Override
    public void deleteById(String id) {
        this.removeById(id);
    }

    @Override
    public void sendCareReminder(String id) {
        // 更新关怀记录状态为已发送
        CustomerCareReminder entity = this.getById(id);
        if (entity != null) {
            entity.setStatus("已发送");
            this.updateById(entity);
        }
    }

    @Transactional
    @Override
    public void batchGenerateCareReminder(clientCareReminderVo vo) {
        // 发送方式去重、去空、排序
        String sendMethod = vo.getSendMethod();
        if (sendMethod != null && !sendMethod.isEmpty()) {
            sendMethod = java.util.Arrays.stream(sendMethod.split(","))
                .map(String::trim)
                .filter(s -> !s.isEmpty())
                .distinct()
                .sorted()
                .reduce((a, b) -> a + "," + b).orElse("");
            vo.setSendMethod(sendMethod);
        }
        List<CustomerCareReminder> reminders = customerCareReminderMapper.generateBatchCareReminders(
            vo.getCareType(), vo.getAdvanceDays(), vo.getSendMethod(), vo.getTemplate());
        for (CustomerCareReminder reminder : reminders) {
            this.save(reminder);
        }
    }
}
